//
// Created by liang on 2025/4/5.
//

#ifndef QT6_TEST_EXECUTETHREAD_H
#define QT6_TEST_EXECUTETHREAD_H
#include <QApplication>

#include "WindowControl.h"
#include "TaskWindow.h"

#include <QThread>
#include <QDebug>
#include <utility>
#include <QMutex>
#include <QWaitCondition>
#include "GlobalContext.h"
#include "SSUtil.h"

namespace SS {
    class ExecuteThread : public QThread
    {

    public:
        // explicit ExecuteThread(TaskWindow taskWindow): _taskWindow(std::move(taskWindow)),
        // context(GlobalContext::getInstance()){
        //     initializePicturePath();
        // }
        ExecuteThread(const TaskWindow& taskWindow): _taskWindow(taskWindow),
        context(GlobalContext::getInstance()){
            initializePicturePath();
        }
        void weakAll() {
            qDebug() << "[ExecuteThread] weakAll";
            m_condition.wakeOne();
        }
    protected:
        void run() override {
            qDebug() << "[ExecuteThread] create" << index;
            while (true) {

                {
                    QMutexLocker locker(&m_mutex);
                    while (!context->getIsStartUp()) {
                        m_condition.wait(&m_mutex);
                    }
                }
                QThread::sleep(1);
                qDebug() << "[ExecuteThread] run" << index;
                // do something
                // SSUtil::CaptureWindow(_taskWindow.windowHandle, picturePath.toStdString().c_str());
            }

        };

    private:
        TaskWindow _taskWindow;
        QString picturePath;
        QMutex m_mutex;
        QWaitCondition m_condition;
        GlobalContext *context;
        int index;
        void initializePicturePath() {
            index = GlobalContext::getInstance()->getIndex();
            picturePath = QCoreApplication::applicationDirPath() + "/windowCapture/" + QString::number(index) +".png";
        }
    };
}



#endif //QT6_TEST_EXECUTETHREAD_H
